﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Library;
using System.ComponentModel;
using OnlineVideos.Data;
using Microsoft.Phone.Controls;
using OnlineVideos.UI;
using System.Windows.Navigation;
//using MyToolkit.Multimedia;
using Common.Utilities;
using OnlineVideos.Entities;


namespace OnlineVideos
{
    public partial class FavoriteVideos : UserControl
    {
        #region GlobalDeclaration

        List<ShowLinks> showFavVideos = null;
        private bool IsDataLoaded;
        #endregion

        #region Constructor

        public FavoriteVideos()
        {
            InitializeComponent();
            IsDataLoaded = false;
            showFavVideos = new List<ShowLinks>();
        }

        #endregion

        #region PageLoad

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsDataLoaded == false)
                {
                    GetPageDataInBackground();
                    IsDataLoaded = true;
                }
            }
            catch (Exception ex)
            {                
                 Exceptions.SaveOrSendExceptions("Exception in UserControl_Loaded Method In FavoriteVideos.cs file.", ex);
            }
        }

        #endregion

        #region Events

        private void lbxFavoritesongs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxFavoritesongs.SelectedIndex == -1)
                    return;
                AppSettings.LinkTitle = (lbxFavoritesongs.SelectedItem as ShowLinks).Title.ToString();
                AppSettings.LinkType = (lbxFavoritesongs.SelectedItem as ShowLinks).LinkType.ToString();
                string Url = (lbxFavoritesongs.SelectedItem as ShowLinks).LinkUrl;
               // MyToolkit.Multimedia.YouTube.LoadYoutubeVideo(Url);
                if (ResourceHelper.AppName == Apps.Video_Mix.ToString())
                {
                   
                    PageHelper.NavigateToDetailPage(AppResources.DetailPageName, AppSettings.ShowID);

                }
                else
                {
                    PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
                    Page p = frame.Content as Page;
                    p.NavigationService.Navigate(new Uri("/Views/Youtube.xaml?myid=" + Url, UriKind.Relative));
                    //YoutubeApi.Youtube.PlayYoutubeVideo(Url);
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxFavoritesongs_SelectionChanged Method In FavoriteVideos.cs file.", ex);
            }
            lbxFavoritesongs.SelectedIndex = -1;
        }

        private void btnFavSong_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OnlineVideos.View_Models.ShowDetail detailModel = new OnlineVideos.View_Models.ShowDetail();
                detailModel.AddToFavorites(lbxFavoritesongs, sender as MenuItem, LinkType.Songs);
                PageHelper.NavigateTo(NavigationHelper.MainPanoramaPage);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in btnFavSong_Click Method In FavoriteVideos.cs file.", ex);
            }
        }

        #endregion

        #region "Common Methods"

        private void GetPageDataInBackground()
        {
            try
            {
                BackgroundHelper bwHelper = new BackgroundHelper();
                bwHelper.AddBackgroundTask(
                                             (object s, DoWorkEventArgs a) =>
                                             {
                                                 a.Result = FavoritesManager.GetFavoriteLinks(LinkType.Songs);
                                             },
                                                   (object s, RunWorkerCompletedEventArgs a) =>
                                                   {
                                                       showFavVideos = (List<ShowLinks>)a.Result;
                                                       if (showFavVideos.Count > 0)
                                                       {
                                                           lbxFavoritesongs.ItemsSource = showFavVideos;

                                                           tblkFavNoSongs.Visibility = Visibility.Collapsed;
                                                       }
                                                       else
                                                       {
                                                           tblkFavNoSongs.Text = "No " + AppResources.FavoriteSongsPivotTitle + " available";
                                                           tblkFavNoSongs.Visibility = Visibility.Visible;
                                                       }
                                                   }
                                                 );
                bwHelper.RunBackgroundWorkers();
            }
            catch (Exception ex)
            {
               Exceptions.SaveOrSendExceptions("Exception in GetPageDataInBackground Method In FavoriteVideos.cs file.", ex);
            }
        }

        #endregion

    }
}
